package com.secsbrain.cloudconsole.dal.config;

import java.util.HashSet;
import java.util.Set;

import com.secsbrain.common.redis.RedisOperatorSentinelImpl;
import com.secsbrain.common.redis.RedisOperatorSingleImpl;
import com.secsbrain.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {

	@Value("${redis.mode:single}")
	private String mode;

	//sentinel
	@Value("${spring.redis.sentinel.master:monito_prod6012}")
	private String sentinelMaster;

	@Value("${spring.redis.sentinel.nodes:172.16.188.108:26012,172.16.188.109:26012,172.16.188.179:26012}")
	private String sentinelNodes;

	@Value("${spring.redis.pool.max-wait:-1}")
	private int maxWaitMillis;

	@Value("${spring.redis.pool.max-idle:8}")
	private int maxIdle;

	@Value("${spring.redis.host:127.0.0.1}")
	private String hostName;

	@Value("${spring.redis.port:6379}")
	private int port;

	@Value("${spring.redis.password:}")
	private String password;

	@Value("${spring.redis.timeout:20000}")
	private int timeout;

	@Value("${spring.redis.database:0}")
	private int index;

	@Bean
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMaxTotal(-1);
		jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
		jedisPoolConfig.setTestOnBorrow(true);
		return jedisPoolConfig;
	}

	@Bean
	public JedisConnectionFactory jedisConnectionFactory() {
		if (!"single".equals(mode)){
			return null;
		}
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(hostName);
		jedisConnectionFactory.setPort(port);
		jedisConnectionFactory.setPassword(password);
		jedisConnectionFactory.setTimeout(timeout);
		jedisConnectionFactory.setDatabase(index);
		jedisConnectionFactory.setUsePool(true);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
		return jedisConnectionFactory;
	}

	@SuppressWarnings("rawtypes")
	@Bean
	public RedisTemplate redisTemplate() {
		if (!"single".equals(mode)){
			return null;
		}
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(jedisConnectionFactory());
		return redisTemplate;
	}

	@SuppressWarnings("unchecked")
	public RedisOperatorSingleImpl operatorSingleImpl() {
		RedisOperatorSingleImpl operatorSingleImpl = new RedisOperatorSingleImpl();
		operatorSingleImpl.setRedisTemplate(redisTemplate());
		return operatorSingleImpl;
	}

	public RedisOperatorSentinelImpl operatorSentinelImpl() {
		RedisOperatorSentinelImpl operatorSentinelImpl = new RedisOperatorSentinelImpl();
		Set<String> sentinels=new HashSet<String>();
		for(String sentinelNode:sentinelNodes.split(",")){
			sentinels.add(sentinelNode);
		}
		operatorSentinelImpl.setJedisSentinleConfig(sentinelMaster, sentinels, jedisPoolConfig(),timeout,password);
		return operatorSentinelImpl;
	}

	@SuppressWarnings("static-access")
	@Bean
	public RedisUtil redisUtil() {
		RedisUtil redisUtil = new RedisUtil();
		if ("cluster".equals(mode)) {
//			redisUtil.setRedisOperator(operatorSingleImpl());
		} else if ("sentinel".equals(mode)) {
			redisUtil.setRedisOperator(operatorSentinelImpl());
		} else {
			redisUtil.setRedisOperator(operatorSingleImpl());
		}
		return redisUtil;
	}
}
